#encoding: utf-8
class Cy < BizUnit
  has_many :bls

  #查询该场站所有提单中的航线汇总
  def routes
    bls = self.bls.select("distinct route").order("route")
    routes = Array.new
    bls.each { |bl| routes << bl.route }
    routes
  end

  #查询该场站所有提单中的航向汇总
  def courses
    bls = self.bls.select("distinct course").order("course")
    courses = Array.new
    bls.each { |bl| courses << bl.course }
    courses
  end

  #查询该场站所有提单中的航次汇总
  def voyages
    logger.debug "航次汇总"
    bls = self.bls.order("voyage").select("distinct voyage")
    courses = Array.new
    bls.each { |bl| courses << bl.voyage }
    courses
  end

  #查询该场站所有提单中的货代汇总
  def forwards
    logger.debug "货代汇总"
    Forward.joins("right outer join bls on biz_units.id = bls.forward_id").where("bls.cy_id" => self.id).group("bls.forward_id").order("biz_units.code")
  end

  #查询该场站所有提单中的港口汇总
  def ports
    logger.debug "港口汇总"
    Port.joins("right outer join bls on biz_units.id = bls.port_id").where("bls.cy_id" => self.id).group("bls.port_id").order("biz_units.code")
  end

  #查询该场站所有提单中的船公司汇总
  def shipowners
    logger.debug "船公司汇总"
    Shipowner.joins("right outer join bls on biz_units.id = bls.shipowner_id").where("bls.cy_id" => self.id).group("bls.shipowner_id").order("biz_units.code")
  end

  #校验报文
  def check_datagram?(datagram)
    #todo校验
    return true
  end

  #报文解析
  def read_datagram(datagram)
    logger.debug "read_data_gram_child"
    logger.debug check_datagram?(datagram)
    if check_datagram?(datagram)
      datagram.each do |data|
        #todo子类实现
        logger.debug "#{data}"
        logger.debug "------==-----------------begin"
        logger.debug "#{data["command"]}"
        case data["command"].to_s
          when "bind_ctn" #配箱

            bind_ctn(data)

          when "remove_part_ctn" #部分撤仓

            remove_part_ctn(data)

          when "modify_ctn" #集装箱修改

            modify_ctn(data)

          when "add_ctn" #新增集装箱
            add_ctn(data)

          when "out_cy" #出场
            out_cy(data)

          when "in_cy" #入场
            in_cy(data)

          when "deliver_to_client" #送抵客户
            deliver_to_client(data)

          when "centralize_on_port" #集港
            centralize_on_port(data)

          when "export_arrive_port" #到港
            export_arrive_port(data)

          when "arrive_cy" #运抵场站
            arrive_cy(data)

          when "check" #查验
            check(data)

          when "release" #放行
            release(data)

          else
            logger.debug "wrong command!"
        end
      end

      logger.debug "------------==---------end"
    end
  end

  #配箱
  def bind_ctn(datagram)
    logger.debug "配箱"
    bl = Bl.find_by_bl_no(datagram["bl_no"])
    if bl
      datagram["ctns"].each do |ctn|
        bl.bind_ctn(ctn['ctn_type'], ctn['ctn_no'])
      end
      logger.debug "成功配箱--succeed bind_ctn!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #部分撤仓
  def remove_part_ctn(datagram)
    logger.debug "部分撤仓"
    bl = Bl.find_by_bl_no(datagram["bl_no"])
    if bl
      datagram['ctns'].each do |ctn|
        bl.remove_part_ctn(ctn['ctn_no'])
      end
      logger.debug "部分撤仓成功--succeed remove_part_ctn!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #集装箱修改
  def modify_ctn(datagram)
    logger.debug "集装箱修改"
    bl = Bl.find_by_bl_no(datagram["bl_no"])
    if bl
      bl.modify_ctn(datagram["old_ctn_no"], datagram['ctn'])
      logger.debug "集装箱修改成功--succeed motify_ctn!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #新增集装箱
  def add_ctn(datagram)
    logger.debug "新增集装箱"
    bl = Bl.find_by_bl_no(datagram["bl_no"])
    if bl
      bl.add_ctn(datagram['ctns'])
      #新增集装箱成功
      logger.debug "新增集装箱成功--succeed add_ctn!"
    else
      #没有此提单，输出错误到错误日志
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #出场
  def out_cy(datagram)
    logger.debug "出场"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.out_cy(datagram['ctn_no'], datagram['vehicle_no'])
      logger.debug "出场成功--succeed out_cy!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #入场
  def in_cy(datagram)
    logger.debug "入场"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.in_cy(datagram['ctn_no'], datagram['vehicle_no'])
      logger.debug "入场成功--succeed in_cy!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #送抵客户
  def deliver_to_client(datagram)
    logger.debug "送抵客户"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      if (datagram['ctn_no'])
        bl.deliver_to_client(datagram['ctn_no'], datagram['vehicle_no'])
      end
      logger.debug "送抵客户成功--succeed deliver_to_client!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #集港
  def centralize_on_port(datagram)
    logger.debug "集港"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.centralize_on_port(datagram['ctn_no'], datagram['vehicle_no'])
      logger.debug "集港成功--succeed centralize_on_port!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #到港
  def export_arrive_port(datagram)
    logger.debug "到港"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.export_arrive_port(datagram['ctn_no'], datagram['vehicle_no'])
      logger.debug "到港成功--succeed export_arrive_port!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #运抵场站
  def arrive_cy(datagram)
    logger.debug "运抵场站"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.arrive_cy(datagram['ctn_no'])
      logger.debug "运抵场站成功--succeed arrive_cy!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #查验
  def check(datagram)
    logger.debug "查验"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.check(datagram['ctn_no'])
      logger.debug "查验成功--succeed check!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end

  #放行
  def release(datagram)
    logger.debug "放行"
    bl = Bl.find_by_bl_no(datagram['bl_no'])
    if bl
      bl.release(datagram['ctn_no'])
      logger.debug "放行成功--succeed release!"
    else
      logger.error "wrong bl_no,no such bl!"
    end
  end
end